package com.project.web.admin.service.core.impl;

import com.project.mapper.core.AdminRoleMapper;
import com.project.mapper.core.RoleMapper;
import com.project.mapper.core.RoleMenuMapper;
import com.project.model.core.Role;
import com.project.model.core.RoleMenu;
import com.project.web.admin.service.common.impl.BaseServiceImpl;
import com.project.web.admin.service.core.RoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yun.common.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/21.
 */
@Service("roleServiceImpl")
public class RoleServiceImpl extends BaseServiceImpl<Role, Long> implements RoleService {

    private static final long serialVersionUID = -528169257494446065L;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    /**
     * 根据roleId删除角色以及关系
     *
     * @param roleId
     */
    @Override
    public void deleteRef(Long roleId) {

        //删除roleMenu的外键关联关系
        roleMenuMapper.deleteByRoleId(roleId);

        //删除adminRole的外键关联关系
        adminRoleMapper.deleteByRoleId(roleId);

        //删除role数据
        roleMapper.delete(roleId);

    }

    /**
     * 验证角色编码的唯一性
     *
     * @param role
     * @return
     */
    @Override
    public List<Role> validateRoleCode(Role role) {
        return roleMapper.validateRoleCode(role);
    }

    /**
     * 根据role、menuIds保存数据
     *
     * @param role
     * @param menuIds
     */
    @Override
    public void saveRoleMenu(Role role, String menuIds) {
        //保存role角色对象
        super.save(role);
        //保存角色和权限关系
        if (StringUtils.isNotEmpty(menuIds)) {
            String[] tmp = menuIds.split(",");
            Long[] ids = StringUtil.strArrToLongArr(tmp);
            if (ids.length > 0) {
                this.updateRoleCascadeMenu(role.getId(), ids);
            }
        }
    }

    /**
     * 根据role、menuIds更新数据
     *
     * @param role
     * @param menuIds
     */
    @Override
    public void updateRoleMenu(Role role, String menuIds) {
        //保存role角色对象
        super.update(role);

        //保存角色和权限关系
        if (StringUtils.isNotEmpty(menuIds)) {
            String[] tmp = menuIds.split(",");
            Long[] idArr = StringUtil.strArrToLongArr(tmp);
            updateRoleCascadeMenu(role.getId(), idArr);
        } else {
            updateRoleCascadeMenu(role.getId(), new Long[0]);
        }

    }

    /**
     * 根据roleId查询menuIds
     *
     * @param roleId
     * @return
     */
    @Override
    public String selectMenuIds(Long roleId) {
        List<Long> menuIds = roleMapper.selectMenuIds(roleId);
        StringBuffer menuIdstr = new StringBuffer();
        for (int i = 0; i < menuIds.size(); i++) {
            if (i != menuIds.size() - 1) {
                menuIdstr.append(menuIds.get(i)).append(",");
            } else {
                menuIdstr.append(menuIds.get(i));
            }
        }
        return menuIdstr.toString().equals("") || menuIdstr.toString().equals("null") ? null : menuIdstr.toString();
    }

    public void updateRoleCascadeMenu(Long roleId, Long[] mennuIds) {
        //构建menuList集合
        List<RoleMenu> roleMenuList = new ArrayList();
        if (ArrayUtils.isNotEmpty(mennuIds)) {
            for (Long menuId : mennuIds) {
                RoleMenu roleMenuTemp = new RoleMenu();
                roleMenuTemp.setMenuId(menuId);
                roleMenuTemp.setRoleId(roleId);
                roleMenuList.add(roleMenuTemp);
            }
        }

        //查询RoleId下的资源权限
        RoleMenu preRoleMenu = new RoleMenu();
        preRoleMenu.setRoleId(roleId);
        roleMenuMapper.delete(preRoleMenu);

        //批量插入新的Role Menu关系集合
        if (CollectionUtils.isNotEmpty(roleMenuList)) {
            roleMenuMapper.saveRoleMenus(roleMenuList);
        }
    }

}
